/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.service;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.entity.MobileUser;
import com.jeeplus.modules.guide.mapper.MobileLoginMapper;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class MobileLoginService {
    private final MobileLoginMapper mobileLoginMapper;
    private final UserService userService;
    public Map<String, Object> selectUser(String OpendId){return mobileLoginMapper.selectUser(OpendId);}
    public Map<String, String> findUser(String loginName){
        return mobileLoginMapper.findUser(loginName);
    }
    public MobileUser findUserMatter(String loginName){
        return mobileLoginMapper.findUserMatter(loginName);
    }
    @Transactional(readOnly = false)
    public int updateUserMatter(MobileUser mu, User users){
        try{
            mobileLoginMapper.updateUserMatter(mu);
            userService.updateUser(users);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }


    public String getOpenid(String idcard){
        return mobileLoginMapper.getOpenid(idcard);
    }

    @Transactional(readOnly = false)
    public void intoUserToken(String id,String token,String loginName){
        mobileLoginMapper.intoUserToken(id,token,DateUtils.getDateTime(),loginName);
    }

    @Transactional(readOnly = false)
    public void intoQRUserToken(String id,String token,String loginName){
        mobileLoginMapper.intoQRUserToken(id,token,DateUtils.getDateTime(),loginName);
    }

    @Transactional(readOnly = false)
    public String getTokenById(String id){
        String result = mobileLoginMapper.getTokenById(id);
        if(StringUtils.isNotBlank(result)){
            mobileLoginMapper.updateTokenById(id);
        }
        return  result;
    }
    @Transactional(readOnly = false)
    public void intoUserCount(String visitTime){
        if(mobileLoginMapper.updateUserCount(visitTime) <= 0){
            mobileLoginMapper.intoUserCount(visitTime);
        }
    }
    @Transactional(readOnly = false)
    public void intoYmbcsCount(String visitTime){
        if(mobileLoginMapper.updateYmbcsCount(visitTime) <= 0){
            mobileLoginMapper.intoYmbcsCount(visitTime);
        }
    }
    @Transactional(readOnly = false)
    public void updateUserValue(String uuid){
        mobileLoginMapper.updateUserValue(uuid);
    }
    public Integer findUserValue(String tell){
        return mobileLoginMapper.findUserValue(tell);
    }

    public Integer findUserValueMonth(String tell){
        return mobileLoginMapper.findUserValueMonth(tell);
    }


    public Integer findUserValueNum(){
        return mobileLoginMapper.findUserValueNum();
    }

    @Transactional(readOnly = false)
    public void intoOtherAllValue(String gxId,String idcard,String name,String address,String openid,String tell,String bizToken){
        mobileLoginMapper.intoOtherAllValue(gxId,idcard,name,address,openid,tell,bizToken);
    }

    @Transactional(readOnly = false)
    public void intoUserMaterial(String materialName,String materialurl,String userId){
        mobileLoginMapper.intoUserMaterial(materialName,materialurl,userId);
    }

    @Transactional(readOnly = false)
    public int updateUserMaterial(String materialurl,String userId,String materialName){
        return mobileLoginMapper.updateUserMaterial(materialurl,userId,materialName);
    }
    @Transactional(readOnly = false)
    public int delUserMaterial(String userId){
        return mobileLoginMapper.delUserMaterial(userId);
    }

    public Integer findUserTell(String tell){
        return  mobileLoginMapper.findUserTell(tell);
    }

    public Integer findUserByIdacrd(String idcard){
        return mobileLoginMapper.findUserByIdacrd(idcard);
    }

    public Map<String, String> findCompanyValue(String openid){
        return mobileLoginMapper.findCompanyValue(openid);
    }
    public Map<String, String> findLegalValue(String openid){
        return mobileLoginMapper.findLegalValue(openid);
    }
    public Map<String, String> findUserByOpendId(String openid){
        return mobileLoginMapper.findUserByOpendId(openid);
    }
}
